package com.cvte.vdt.uartservice.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.cvte.vdt.uartservice.util.DataUtil;
import com.cvte.vdt.uartservice.util.LogUtil;
import com.cvte.vdt.uartservice.util.SQLiteUtil;
import com.cvte.vdtlibrary.bluetooth.EntityBluetoothDevice;
import com.cvte.vdtlibrary.bluetooth.EnumBtDeviceStatus;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by wuguitong on 2018/12/1.
 */
public class BluetoothDevDaoSQliteImp implements BluetoothDevDao{
    private final static String TAG = "BluetoothDevDaoSQliteImp";
    private Context context;
    private SQLiteUtil dbUtil;
    private SQLiteDatabase db;
    private Object lock = new Object();

    public BluetoothDevDaoSQliteImp(Context context) {
        dbUtil = new SQLiteUtil(context);
    }

    @Override
    public List<EntityBluetoothDevice> getAllBtDevList() {
        synchronized (lock) {
            List<EntityBluetoothDevice> list = new ArrayList<EntityBluetoothDevice>();
            db = dbUtil.getReadableDatabase();
            Cursor cr = db.rawQuery("select * from " + SQLiteUtil.DB_TABLE_NAME, null);
            while (cr.moveToNext()) {
                EntityBluetoothDevice dev = new EntityBluetoothDevice(cr.getString(cr.getColumnIndex(SQLiteUtil.BT_DEV_MAC)),
                        cr.getString(cr.getColumnIndex(SQLiteUtil.BT_DEV_NAME)), EnumBtDeviceStatus.values()[cr.getInt(cr.getColumnIndex(SQLiteUtil.BT_STATUS))]);
                list.add(dev);
            }
            cr.close();
            db.close();
            return list;
        }
    }

    @Override
    public List<EntityBluetoothDevice> getPairBtDevList() {
        synchronized (lock) {
            List<EntityBluetoothDevice> list = new ArrayList<EntityBluetoothDevice>();
            db = dbUtil.getReadableDatabase();
            Cursor cr = db.rawQuery("select * from " + SQLiteUtil.DB_TABLE_NAME + " where " + SQLiteUtil.BT_STATUS + " != ?", new String[]{String.valueOf(EnumBtDeviceStatus.DEV_NOT_PAIR.ordinal())});
            while (cr.moveToNext()) {
                EntityBluetoothDevice dev = new EntityBluetoothDevice(cr.getString(cr.getColumnIndex(SQLiteUtil.BT_DEV_MAC)),
                        cr.getString(cr.getColumnIndex(SQLiteUtil.BT_DEV_NAME)), EnumBtDeviceStatus.values()[cr.getInt(cr.getColumnIndex(SQLiteUtil.BT_STATUS))]);
                list.add(dev);
            }
            cr.close();
            db.close();
            return list;
        }
    }

    @Override
    public List<EntityBluetoothDevice> getNotPairBtDevList() {
        synchronized (lock) {
            List<EntityBluetoothDevice> list = new ArrayList<EntityBluetoothDevice>();
            db = dbUtil.getReadableDatabase();
            Cursor cr = db.rawQuery("select * from " + SQLiteUtil.DB_TABLE_NAME + " where " + SQLiteUtil.BT_STATUS + " = ?", new String[]{String.valueOf(EnumBtDeviceStatus.DEV_NOT_PAIR.ordinal())});
            while (cr.moveToNext()) {
                EntityBluetoothDevice dev = new EntityBluetoothDevice(cr.getString(cr.getColumnIndex(SQLiteUtil.BT_DEV_MAC)),
                        cr.getString(cr.getColumnIndex(SQLiteUtil.BT_DEV_NAME)), EnumBtDeviceStatus.values()[cr.getInt(cr.getColumnIndex(SQLiteUtil.BT_STATUS))]);
                list.add(dev);
            }
            cr.close();
            db.close();
            return list;
        }
    }

    @Override
    public EntityBluetoothDevice getBtDevByKey(String key) {
        synchronized (lock) {
            db = dbUtil.getReadableDatabase();
            Cursor cr = db.rawQuery("select * from " + SQLiteUtil.DB_TABLE_NAME + " where " + SQLiteUtil.BT_DEV_MAC + " = ?", new String[]{key});
            while (cr.moveToNext()) {
                EntityBluetoothDevice dev = new EntityBluetoothDevice(cr.getString(cr.getColumnIndex(SQLiteUtil.BT_DEV_MAC)),
                        cr.getString(cr.getColumnIndex(SQLiteUtil.BT_DEV_NAME)), EnumBtDeviceStatus.values()[cr.getInt(cr.getColumnIndex(SQLiteUtil.BT_STATUS))]);
                return dev;

            }
            return null;
        }
    }

    @Override
    public int cleanBtDev() {
        synchronized (lock){
            db = dbUtil.getWritableDatabase();
            db.execSQL("delete from " + SQLiteUtil.DB_TABLE_NAME);
            db.close();
            return 0;
        }
    }

    @Override
    public int cleanNotPairBtDev() {
        synchronized (lock){
            db = dbUtil.getWritableDatabase();
            db.execSQL("delete from " + SQLiteUtil.DB_TABLE_NAME + " where " + SQLiteUtil.BT_STATUS + "=?",new Object[]{EnumBtDeviceStatus.DEV_NOT_PAIR.ordinal()});
            db.close();
            return 0;
        }
    }

    @Override
    public boolean initBtDevStatus() {
        synchronized (lock){
            db = dbUtil.getWritableDatabase();
            db.execSQL("update " + SQLiteUtil.DB_TABLE_NAME + " set " + SQLiteUtil.BT_STATUS + " =? where " + SQLiteUtil.BT_STATUS + "=? or " + SQLiteUtil.BT_STATUS + "=? or " + SQLiteUtil.BT_STATUS + "=?",
                    new Object[]{EnumBtDeviceStatus.DEV_PAIR.ordinal(), EnumBtDeviceStatus.DEV_CONNECTED.ordinal(),EnumBtDeviceStatus.DEV_CONNECTING.ordinal(),EnumBtDeviceStatus.DEV_DIS_CONNECTING.ordinal()});
            db.close();
            return true;
        }
    }

    @Override
    public boolean updateBtDevPairStatus(String key ,EnumBtDeviceStatus status) {
        synchronized (lock){
            db = dbUtil.getWritableDatabase();
            db.execSQL("update " + SQLiteUtil.DB_TABLE_NAME + " set " + SQLiteUtil.BT_STATUS + " =? where " + SQLiteUtil.BT_DEV_MAC + "=?", new Object[]{status.ordinal(),key});
            db.close();
            return true;
        }
    }

    @Override
    public boolean updateBtDevName(String key, String name) {
        synchronized (lock){
            db = dbUtil.getWritableDatabase();
            db.execSQL("update " + SQLiteUtil.DB_TABLE_NAME + " set " + SQLiteUtil.BT_DEV_NAME + " =? where " + SQLiteUtil.BT_DEV_MAC + "=?", new Object[]{name,key});
            db.close();
            return true;
        }
    }

    @Override
    public boolean addBtDev(String key, String name, EnumBtDeviceStatus status) {
        synchronized (lock){
            boolean res = false;
            db = dbUtil.getReadableDatabase();
            Cursor cr = db.rawQuery("select * from " + SQLiteUtil.DB_TABLE_NAME + " where " + SQLiteUtil.BT_DEV_MAC + " = ?", new String[]{key});
            if (cr.getCount() > 0) {
                res = true;
            }
            cr.close();
            db.close();
            if(res != true) {
                LogUtil.d(TAG,"addBtDev" + key);
                db = dbUtil.getWritableDatabase();
                db.execSQL("insert into " + SQLiteUtil.DB_TABLE_NAME + " ("
                        + SQLiteUtil.BT_DEV_MAC + ","
                        + SQLiteUtil.BT_DEV_NAME + ","
                        + SQLiteUtil.BT_STATUS
                        + ") values (?,?,?)", new Object[]{key, name, status.ordinal()});
                db.close();
            }
            return true;
        }
    }

    @Override
    public boolean addBtDev(String key) {
        return addBtDev(key,DataUtil.DEFAULT_NAME,EnumBtDeviceStatus.DEV_NOT_PAIR);
    }

    @Override
    public boolean deleteBtDev(String key) {
        synchronized (lock) {
            db = dbUtil.getWritableDatabase();
            int count = db.delete(SQLiteUtil.DB_TABLE_NAME, SQLiteUtil.BT_DEV_MAC + " = ?", new String[]{key});
            db.close();
            if (count == 1) {
                return true;
            } else {
                return false;
            }
        }
    }

    @Override
    public boolean isContainBtDev(String key) {
        synchronized (lock) {
            boolean res = false;
            db = dbUtil.getReadableDatabase();
            Cursor cr = db.rawQuery("select * from " + SQLiteUtil.DB_TABLE_NAME + " where " + SQLiteUtil.BT_DEV_MAC + " = ?", new String[]{key});
            if (cr.moveToNext()) {
                res = true;
            }
            cr.close();
            db.close();
            return res;
        }
    }
}
